package com.utooo.service.system;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.utooo.dao.purchase.ContractAcceptanceTypeMapper;
import com.utooo.dao.purchase.ContractSourceConfigMapper;
import com.utooo.dao.purchase.ContractTypeMapper;
import com.utooo.dao.purchase.DemandTypeDefinitionMapper;
import com.utooo.dao.purchase.OrderSourceMapper;
import com.utooo.dao.purchase.OrderTypeMapper;
import com.utooo.dao.purchase.PriceTypeMapper;
import com.utooo.dao.purchase.ProcurementStrategySettingMapper;
import com.utooo.dao.system.BrandMapper;
import com.utooo.dao.system.CategoryMapper;
import com.utooo.dao.system.CompanyMapper;
import com.utooo.dao.system.MaterialMapper;
import com.utooo.dao.system.PurchaserMapper;
import com.utooo.dao.system.TaxRateMapper;
import com.utooo.dao.system.UnitMapper;
import com.utooo.pojo.domain.purchase.config.ContractAcceptanceTypeDO;
import com.utooo.pojo.domain.purchase.config.ContractSourceConfigDO;
import com.utooo.pojo.domain.purchase.config.ContractTypeDO;
import com.utooo.pojo.domain.purchase.config.OrderSourceDO;
import com.utooo.pojo.domain.purchase.config.OrderTypeDO;
import com.utooo.pojo.domain.purchase.config.PriceTypeDO;
import com.utooo.pojo.domain.purchase.config.ProcurementStrategySettingDO;
import com.utooo.pojo.domain.system.BrandDO;
import com.utooo.pojo.domain.system.CategoryDO;
import com.utooo.pojo.domain.system.CompanyDO;
import com.utooo.pojo.domain.system.MaterialDO;
import com.utooo.pojo.domain.system.PurchaserDO;
import com.utooo.pojo.domain.system.TaxRateDO;
import com.utooo.pojo.domain.system.UnitDO;
import com.utooo.pojo.vo.common.BaseMapVO;
import com.utooo.pojo.vo.common.SystemEnumsResponseVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author CK
 * @Date: 2025−04-17 14:58
 * @Description: SystemServiceImpl
 * @Copyright: 南京优通信息科技股份有限公司
 */
@Service
@RequiredArgsConstructor
public class SystemServiceImpl implements SystemService {

    private final BrandMapper brandMapper;
    private final CategoryMapper categoryMapper;
    private final CompanyMapper companyMapper;
    private final MaterialMapper materialMapper;
    private final PurchaserMapper purchaserMapper;
    private final UnitMapper unitMapper;
    private final ProcurementStrategySettingMapper procurementStrategySettingMapper;
    private final DemandTypeDefinitionMapper demandTypeDefinitionMapper;
    private final OrderTypeMapper orderTypeMapper;
    private final OrderSourceMapper orderSourceMapper;
    private final ContractSourceConfigMapper contractSourceConfigMapper;
    private final TaxRateMapper taxRateMapper;
    private final ContractTypeMapper contractTypeMapper;
    private final ContractAcceptanceTypeMapper contractAcceptanceTypeMapper;
    private final PriceTypeMapper priceTypeMapper;

    @Override
    public SystemEnumsResponseVO getSystemEnums() {
        SystemEnumsResponseVO systemEnumsResponseVO = new SystemEnumsResponseVO();
        List<BrandDO> brands = brandMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(brands)) {
            List<BaseMapVO> brandList = new ArrayList<>();
            brands.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    brandList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setBrandList(brandList);
        }
        List<CategoryDO> categories = categoryMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(categories)) {
            List<BaseMapVO> categoryList = new ArrayList<>();
            categories.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    categoryList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setMainCategoryList(categoryList);
        }
        List<CompanyDO> companies = companyMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(companies)) {
            List<BaseMapVO> companyList = new ArrayList<>();
            companies.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    companyList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setCompanyList(companyList);
        }
        List<MaterialDO> materials = materialMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(materials)) {
            List<BaseMapVO> materialList = new ArrayList<>();
            materials.forEach(i -> {
                String code = i.getCode();
                if (StrUtil.isNotBlank(code)) {
                    materialList.add(new BaseMapVO(i.getId(), code));
                }
            });
            systemEnumsResponseVO.setMaterialCodeList(materialList);
        }
        List<PurchaserDO> purchasers = purchaserMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(purchasers)) {
            List<BaseMapVO> purchaserList = new ArrayList<>();
            purchasers.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    purchaserList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setPurchaserList(purchaserList);
        }
        List<UnitDO> units = unitMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(units)) {
            List<BaseMapVO> unitList = new ArrayList<>();
            units.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    unitList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setUnitList(unitList);
        }
        List<ProcurementStrategySettingDO> procurementStrategies = procurementStrategySettingMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(procurementStrategies)) {
            List<BaseMapVO> procurementStrategyList = new ArrayList<>();
            procurementStrategies.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    procurementStrategyList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setPurchaseStrategyList(procurementStrategyList);
        }
        List<OrderTypeDO> orderTypes = orderTypeMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(orderTypes)) {
            List<BaseMapVO> orderTypeList = new ArrayList<>();
            orderTypes.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    orderTypeList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setOrderTypeList(orderTypeList);
        }
        List<OrderSourceDO> orderSources = orderSourceMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(orderSources)) {
            List<BaseMapVO> orderSourceList = new ArrayList<>();
            orderSources.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    orderSourceList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setOrderSourceList(orderSourceList);
        }
        List<ContractSourceConfigDO> contractSourceConfigs = contractSourceConfigMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(contractSourceConfigs)) {
            List<BaseMapVO> contractSourceConfigList = new ArrayList<>();
            contractSourceConfigs.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    contractSourceConfigList.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setContractSourceList(contractSourceConfigList);
        }
        List<TaxRateDO> taxRates = taxRateMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(taxRates)) {
            List<BaseMapVO> taxRateList = new ArrayList<>();
            taxRates.forEach(i -> {
                String code = i.getCode();
                if (StrUtil.isNotBlank(code)) {
                    taxRateList.add(new BaseMapVO(i.getId(), code));
                }
            });
            systemEnumsResponseVO.setTaxRateList(taxRateList);
        }
        List<ContractTypeDO> contractTypes = contractTypeMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(contractTypes)) {
            List<BaseMapVO> types = new ArrayList<>();
            contractTypes.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    types.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setContractTypeList(types);
        }
        List<ContractAcceptanceTypeDO> contractAcceptanceTypes = contractAcceptanceTypeMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(contractAcceptanceTypes)) {
            List<BaseMapVO> types = new ArrayList<>();
            contractAcceptanceTypes.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    types.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setContractAcceptanceTypeList(types);
        }
        List<PriceTypeDO> paymentTypes = priceTypeMapper.selectList(new QueryWrapper<>());
        if (CollUtil.isNotEmpty(paymentTypes)) {
            List<BaseMapVO> types = new ArrayList<>();
            paymentTypes.forEach(i -> {
                String name = i.getName();
                if (StrUtil.isNotBlank(name)) {
                    types.add(new BaseMapVO(i.getId(), name));
                }
            });
            systemEnumsResponseVO.setPaymentTypeList(types);
        }
        return systemEnumsResponseVO;
    }
}
